<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">jQuery.ajaxSetup()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/ajax/">Ajax</a> &gt; <a href="/category/ajax/low-level-interface/">底层接口</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/jQuery.ajaxSetup/" target="_blank">jQuery.ajaxSetup()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="jQuery-ajaxSetup1"><h2 class="section-title">
<span class="name">jQuery.ajaxSetup( options )</span><span class="returns">返回: <a href="/Types/#"></a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>为以后要用到的Ajax请求设置默认的值</p>
<ul class="signatures"><li class="signature" id="jQuery-ajaxSetup-options">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.1/">1.1</a></span>jQuery.ajaxSetup( options )</h4>
<ul><li>
<div><strong>options</strong></div>
<div>类型: <a href="/Types/#PlainObject">PlainObject</a>
</div>
<div>一个用来配置Ajax请求的"{键:值}"对，所有的选项都是可选的。</div>
</li></ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p>用于设置<code>$.ajaxSetup()</code>的详细参数, 参见 <code><a href="/jQuery.ajax">$.ajax()</a></code>. </p>
<p>所有后面的 Ajax 调用任何函数都将使用新的设置参数,除非它们调用时设置了各自的参数重载了这个默认值。 直到下一次调用<code>$.ajaxSetup()</code>.</p>
<p><strong>注意：</strong>
			        此处指定的设置会影响<em>所有</em> <code>$.ajax</code>或基于AJAX的衍生方法，如<code>$.get()</code>的调用。这可能会导致不良的行为因为其他调用（例如，插件）可能希望正常的默认设置。出于这个原因，我们强烈建议您不要使用此API。相反我们建议，在调用时明确设置选项或定义一个简单的插件。</p>
<p>举个例子,我们可以事先设置服务器重复响应的默认URL参数：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$.ajaxSetup({</code></div></div><div class="container"><div class="line"><code>  url: <span class="string">'ping.php'</span></code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>现在每次Ajax请求将自动使用这个"ping.php" URL：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$.ajax({</code></div></div><div class="container"><div class="line"><code>  <span class="comment">// url not set here; uses ping.php</span></code></div></div><div class="container"><div class="line"><code>  data: {<span class="string">'name'</span>: <span class="string">'Dan'</span>}</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<div class="warning">
<p><strong>注意</strong>: 全局回调函数应使用他们各自的全局Ajax事件处理方法-<code><a href="/ajaxStart">.ajaxStart()</a></code>,
			                <code><a href="/ajaxStop">.ajaxStop()</a></code>, <code><a href="/ajaxComplete">.ajaxComplete()</a></code>, <code><a href="/ajaxError">.ajaxError()</a></code>, <code><a href="/ajaxSuccess">.ajaxSuccess()</a></code>, <code><a href="/ajaxSend">.ajaxSend()</a></code>-设置，而不是为 <code>$.ajaxSetup()</code>
			                设置 <code>options</code> 对象。</p>
</div>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4><span class="desc">设置 AJAX 请求默认地址为 "/xmlhttp/"，禁止触发全局 AJAX 事件，用 POST 代替默认 GET 方法。其后的 AJAX 请求不再设置任何选项参数。</span></h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$.ajaxSetup({</code></div></div><div class="container"><div class="line"><code>   url: <span class="string">"/xmlhttp/"</span>,</code></div></div><div class="container"><div class="line"><code>   global: <span class="literal">false</span>,</code></div></div><div class="container"><div class="line"><code>   type: <span class="string">"POST"</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code> });</code></div></div><div class="container"><div class="line"><code> $.ajax({ data: myData });</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div></section>
</div></article> </div>
</article>